package chat;

import javax.swing.*;
import java.net.Socket;
import java.net.ServerSocket;
import java.io.*;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionEvent;
import java.awt.Container;
import java.awt.BorderLayout;

public class Client extends JFrame implements Runnable
{
	BufferedReader reader;
	PrintWriter writer;
	Socket socket;
	
	JTextField nameBox=new JTextField("请输入你的昵称");
	JTextField sendBox=new JTextField();	
	JPanel panel=new JPanel();
	private final JTextArea messageView = new JTextArea();
	//JScrollPane scrollPane=new JScrollPane(messageView);
	private final JButton btnNewButton = new JButton("发送");
	private final JButton btnNewButton_1 = new JButton("关闭");
	private final JButton btnNewButton_2 = new JButton("加入聊天");
	private final JButton btnNewButton_3 = new JButton("清屏");
	private final JButton button = new JButton("启动服务器");
	public Client()
	{	
		setTitle("chat");
		super.setBounds(300, 200, 512, 383);
		messageView.setEditable(false);
		Container c=getContentPane();
		c.setLayout(null);
		c.setBounds(300, 200, 512, 383);
		nameBox.setBounds(0, 0, 197, 30);
		messageView.setBounds(0, 40, 487, 158);
		sendBox.setBounds(0, 208, 487, 37);
		c.add(nameBox);
		c.add(sendBox);
		c.add(messageView);		
		nameBox.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				nameBox.setText(null);
			}
		});		
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(nameBox.getText()!=null){
					writer.println(nameBox.getText()+":"+sendBox.getText());
					sendBox.setText(null);
				}
			}
		});
		btnNewButton.setBounds(394, 255, 93, 23);
		
		c.add(btnNewButton);
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(1);
			}
		});
		btnNewButton_1.setBounds(291, 255, 93, 23);
		
		c.add(btnNewButton_1);
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				nameBox.getText();
				writer.println(nameBox.getText()+"加入聊天");
			}
		});
		btnNewButton_2.setBounds(207, 3, 93, 23);
		
		c.add(btnNewButton_2);
		btnNewButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				messageView.setText(null);
			}
		});
		btnNewButton_3.setBounds(331, 3, 93, 23);
		
		c.add(btnNewButton_3);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new Server().openServer();
				System.exit(1);
			}
		});
		button.setBounds(10, 255, 93, 23);
		
		getContentPane().add(button);

		sendBox.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{	
				if(nameBox.getText()!=null){
					writer.println(nameBox.getText()+":"+sendBox.getText());
					sendBox.setText(null);
				}
			}
		});

	}
	public void run()
	{
		while(true)
		{
			try
			{
				messageView.append(reader.readLine()+"\n");
			}
			catch(IOException ioe)
			{
			   System.err.println("发生IO异常:"+ioe);
			   ioe.printStackTrace();
			}
			
		}
	}
	public void startClient(String host,int port)
	{
		try
		{
			messageView.append("连接服务器...\n");
			socket =new Socket(host,port);
			messageView.append("连接成功，您可以聊天了！请加入聊天\n");
			if(nameBox.getText()!=null){
				reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
				writer=new PrintWriter(socket.getOutputStream(),true);
				new Thread(this).start();
			}
		}
		catch(Exception e)
		{
			messageView.append("连接失败,运行服务器，并重启\n");
		}
	}
}
